import request from 'umi-request'

export default {
    namespace: 'employee',
    state: [
        
    ],
    reducers: {
        deleteEmployee(state, { payload }) {
            const { key: targetId } = payload;
            return state.filter((e) => e.key !== targetId);
        },
        update(_, { payload }) {
            console.log('update')
            const {employees: newEmployees} = payload
            return newEmployees
        },
        updateEmployee(state, { payload }) {
            console.log('updateEmployee')
            console.log(state)
            console.log({payload})

            const { newEmployees } = payload
            
            if (!newEmployees.key) {
                // return (
                //     state.push(newEmployees)
                // )
            }

            return (
                state.map((e) => (
                    e.key == newEmployees.key ? newEmployees : e
                ))
            )
        },
    },
    effects: {
        *getAll(_, {call, put}) {
            const response = yield call(request.get, '/api/employees')
            yield put({
                type: 'update',
                payload: {
                    employees: response
                }
            })
        },
        *createEmployee(props, {call, put}) {
            // const response = yield call(request.post, '/api/employee/create', newEmployees)
            console.log('createEmployee')
            console.log(props)
        }
    }
}